package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealSercive;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealSercive setmealSercive;

    /**
     * 套餐分页查询
     * @param dto
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO dto){
        log.info("套餐分页查询");
        PageResult pageResult=setmealSercive.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param dto
     * @return
     */
    //@CacheEvict清理指定套餐的缓存
    @CacheEvict(cacheNames = "setmeal",key = "#dto.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO dto){
        log.info("新增套餐");
        setmealSercive.addSetmeal(dto);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param dto
     * @return
     */
    //allEntries = true:清除setmeal下的全部缓存
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result updateSetmwal(@RequestBody SetmealDTO dto){
            log.info("修改套餐：{}", dto);
            setmealSercive.updateSetmeal(dto);
            return Result.success();
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    //allEntries = true:清除setmeal下的全部缓存
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("删除套餐")
    @DeleteMapping
    public Result deleteSET(@RequestParam List<Long> ids){
        log.info("删除套餐，ids：{}", ids);
        setmealSercive.deleteSetmeal(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐,数据回显
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> sel(@PathVariable Long id){
        log.info("根据id查询套餐，id：{}", id);
        SetmealVO setmealVO = setmealSercive.selById(id);
        return Result.success(setmealVO);
    }

    /**
     * 套餐的起售和停售
     * @param status,id
     * @return
     */
    //allEntries = true:清除setmeal下的全部缓存
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("起售停售")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("套餐的起售和停售");
        setmealSercive.startOrStop(status,id);
        return Result.success();
    }


}
